package com.example.rudeness;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;


/**
 * Created by Caodongyao on 2017/8/4.
 */

public final class DensityHelper {
    private DensityHelper() {
    }
    private static final float WIDTH = 750;
    private static final float HEIGHT = 1334;

    public static float rpx(int value, Context context) {
        float wRatioValue = (getDisplayWidthInPx(context) / WIDTH) * value;
        float hRatioValue = (getDisplayHeightInPx(context) / HEIGHT) * value;
        float h = 0;
        if (value <= WIDTH) {
            h = wRatioValue;
        } else if (value > WIDTH) {
            h = hRatioValue;
        }
        return h;
    }

    /**
     * 获取屏幕宽度
     *
     * @return 屏幕宽度
     */
    public static int getDisplayWidthInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().width;
    }

    /**
     * 获取屏幕高度，不包含状态栏的高度
     *
     * @return 屏幕高度，不包含状态栏的高度
     */
    public static int getDisplayHeightInPx(Context context) {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().height;
    }

    /**
     * vp转像素
     *
     * @param vp
     * @return
     */
    public static float vp2px(float vp, Context context) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes();
        return (attributes.densityPixels * vp);
    }
}
